package com.jkt.rms.validator;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.jkt.rms.vo.RateVO;

public class RateValidator implements Validator {
	
	@Override
	public boolean supports(Class clazz) {
		return clazz.equals(RateVO.class);
		
	}
	
	public void validate(Object obj, Errors errors) {
		RateVO rate = (RateVO) obj;
		if (rate == null) {
			errors.rejectValue("usrid", "error.login.not-specified", null,"Value required.");
			return;
		}
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "vendorCode", "error.mandatory.field", new String[]{"Vendor"});
		if(rate.getVendorCode().equals("0")){
			errors.rejectValue("vendorCode", "error.mandatory.field", new String[]{"Vendor"}, null);
		}
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "roleCode", "error.mandatory.field", new String[]{"Role"});
		if(rate.getRoleCode().equals("0")){
			errors.rejectValue("roleCode", "error.mandatory.field", new String[]{"Role"}, null);
		}
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "locationCode", "error.mandatory.field", new String[]{"Location"});
		if(rate.getLocationCode().equals("0")){
			errors.rejectValue("locationCode", "error.mandatory.field", new String[]{"Location"}, null);
		}
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startdateStr", "error.mandatory.field", new String[]{"Start Date"});
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "enddateStr", "error.mandatory.field", new String[]{"End Date"});
		if(rate.getStartdateStr()!=null && rate.getEnddateStr()!= null && !rate.getStartdateStr().equals("") && !rate.getEnddateStr().equals("")){
			SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
			try {
				Date startDate = format.parse(rate.getStartdateStr());
				Date endDate = format.parse(rate.getEnddateStr());
				
				Calendar cal = Calendar.getInstance();
				int year = cal.get(Calendar.YEAR);
				
				Calendar calLastYear = Calendar.getInstance();
				calLastYear.set(year, Calendar.FEBRUARY, 1);
				
				Calendar calLastYearLastDay = Calendar.getInstance();
				calLastYearLastDay.set(year, Calendar.FEBRUARY, calLastYear.getActualMaximum(Calendar.DAY_OF_MONTH));
				
				Calendar calNextYear = Calendar.getInstance();
				calNextYear.set(year+1, Calendar.FEBRUARY, 1);
				
				Calendar calNextYearLastDay = Calendar.getInstance();
				calNextYearLastDay.set(year+1, Calendar.FEBRUARY, calNextYear.getActualMaximum(Calendar.DAY_OF_MONTH));
				
				if(startDate.before(calLastYearLastDay.getTime())){
					errors.rejectValue("startdateStr", "rate.startdate.before", null);
				}
				if(startDate.after(calNextYearLastDay.getTime())){
					errors.rejectValue("startdateStr", "rate.startdate.after", null);
				}
				if(endDate.before(startDate)){
					errors.rejectValue("enddateStr", "rate.endate.before", null);
				}
				if(endDate.after(calNextYearLastDay.getTime())){
					errors.rejectValue("enddateStr", "rate.enddate.after", null);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		if(rate.getRate() <= 0.0f){
			errors.rejectValue("rate", "error.mandatory.field", new String[]{"Rate"}, null);
		}
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "currencyId", "error.mandatory.field", new String[]{"Currency"});
		if(rate.getCurrencyId() == 0){
			errors.rejectValue("currencyId", "error.mandatory.field", new String[]{"Currency"}, null);
		}
				
	}
}
